<template>
  <div class="user-info-main">
    <div class="form">
      <el-form :model="user" label-width="100px">
        <el-form-item label="昵称：">
          <el-input v-model="user.nickName" />
        </el-form-item>
        <el-form-item label="用户名：">
          {{ user.mail }}
        </el-form-item>
        <el-form-item label="我的签名：">
          <el-input
            v-model="user.signature"
            type="textarea"
            placeholder="设置您的签名(づ￣ 3￣)づ"
            rows="7"
          />
        </el-form-item>
        <el-form-item label="性别：">
          <el-radio-group v-model="user.sex">
            <el-radio label="1">男</el-radio>
            <el-radio label="0">女</el-radio>
            <el-radio label="2">保密</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <el-divider class="hr" />
    <div class="submit-button">
      <el-button
        type="primary"
        size="large"
        @click="updateUser"
        style="width: 120px"
        >保存</el-button
      >
    </div>
  </div>
</template>

<script>
import UserApi from '@/api/core/User'
export default {
  name: 'UserInfo',
  data() {
    return {
      user: {},
    }
  },

  created() {
    this.getUserInfo()
  },

  methods: {
    getUserInfo() {
      UserApi.getUserInfo().then((response) => {
        this.user = response.data.userInfo
      })
    },

    updateUser() {
      if (this.user.nickName.length < 2 || this.user.nickName.length > 10) {
        this.$message.error('用户昵称要在2~10之间')
        return
      }

      UserApi.updataUser(this.user)
        .then((response) => {
          this.$message.success(response.message)
          this.getUserInfo()
        })
        .catch((error) => {
          this.$message.error(error.message)
        })
    },
  },
}
</script>

<style scoped>
.user-info-main {
  width: 600px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}
.form {
  width: 400px;
  height: auto;
  margin-left: 10%;
}
.hr {
  width: 800px;
  margin-left: 10%;
}
.submit-button {
  display: flex;
  width: 900px;
  justify-content: center;
}
</style>
